import { CdkDropList, CdkDrag } from '@angular/cdk/drag-drop';
import { NgClass, NgIf, NgStyle } from '@angular/common';
import { Component, EventEmitter, Input, OnInit, Output, TemplateRef, ViewChild } from '@angular/core';

import { DraggableDirective } from '@app/shared/directives/draggable.directive';
import { DroppableDirective } from '@app/shared/directives/droppable.directive';
import { ResizableModule } from 'angular-resizable-element';
import { NzSafeAny } from 'ng-zorro-antd/core/types';

import { DashboardWidget } from '../dashboard-widget';
import { WidgetSharedService } from '../widgetShared.service';
import { WidgetConfig, WidgetSetting } from '../widget-default-setting';

@Component({
  selector: 'app-text-widget',
  standalone: true,
  imports: [DraggableDirective, DroppableDirective, CdkDropList, ResizableModule, NgStyle, CdkDrag, NgClass, NgIf],
  templateUrl: './text-widget.component.html',
  styleUrl: './text-widget.component.less'
})
export class TextWidgetComponent extends DashboardWidget implements OnInit {
  @Input() currentWidget!: WidgetConfig;
  @Output() readonly resizeEvent = new EventEmitter();

  constructor(private readonly sharedService: WidgetSharedService) {
    super();
    this.resizeWidgetEvent = this.resizeEvent;
  }

  override getWidget(): WidgetConfig | null {
    return this.currentWidget;
  }

  ngOnInit(): void {
    this.sharedService.initWidget.subscribe((widget: WidgetConfig | null) => {
      if (!this.currentWidget) {
        this.currentWidget = widget!;
      }
    });

    this.sharedService.updateWidget.subscribe((widget: WidgetConfig | null) => {
      if (widget?.tag === this.currentWidget.tag) {
        this.currentWidget = widget!;
      }
    });
  }
}
